package com.mengxuegu.security.authrntication;

import com.alibaba.fastjson.JSON;
import com.mengxuegu.base.result.MengxueguResult;
import com.mengxuegu.security.properites.LoginResponseType;
import com.mengxuegu.security.properites.SecurityProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证成功处理器
 * 1.决定 响应json 还是跳转页面,或者认证成功后进行其他处理
 *
 * @author william
 * @create 2021-07-03 22:57
 **/
@Component("customAuthenticationSuccessHandler")
//public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
public class CustomAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
   Logger logger= LoggerFactory.getLogger(CustomAuthenticationSuccessHandler.class);

    @Autowired
    SecurityProperties securityProperties;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
                                        HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        if (LoginResponseType.JSON.equals(securityProperties.getAuthentication().getLoginType())) {
            //认证成功后,响应json 字符串
            MengxueguResult result = MengxueguResult.ok("认证成功");

            String json = result.toJsonString();
            response.setContentType("application/json;charset=UTF-8");
            response.getWriter().write(json);
        } else {
           //重定向到上次请求的地址上,引发跳转到认证页面的地址
            logger.info("authentication:"+ JSON.toJSONString(authentication));
            super.onAuthenticationSuccess(request,response,authentication);
        }

    }
}
